package com.pubpage.user_group_maintain;

import com.pub.database.*;
import javax.servlet.http.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();

      View view=(View)request.getAttribute("view");


      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataViewForm = new DataViewForm();

      Business business=new Business(gqbnDAO);

      String lstrResult=new String();
      try{
          gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

          DataView = new DataView(gqbnDAO);

          request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
          request.setAttribute("DataViewForm",DataViewForm);

          view.setButtonVisible("0");
          view.setuser_no_readonly(true);
          view.setPrgTitle(gqbnDAO.readSingleValueData("select nvl(data,'人員設定') from sys_ctl where sys='"+view.getSys()+"' and ctl_id='"+view.getCtl_id()+"' and ctl_type='0'"));


         // Add
         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            view.setAllClear();
            //flow.preAddAction();
            view.setButtonVisible("1");
            view.setPrgStatus("add");
         }

         // Save
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
            //lstrResult= flow.addAction();
            view.setTipMessage("請輸入下列欄位，星號為必填欄位。輸入完畢請按存檔");
            view.setOnloadScript("alert('" + lstrResult + "');");
            if (lstrResult.equals("資料新增成功!!")){
               view.setButtonVisible("2");
               view.setPrgStatus("save");
            } else {
                view.setButtonVisible("1");
                view.setuser_no_readonly(false);
                view.setPrgStatus("add");
            }
         }

         //按刪除
         if(view.getPrgAction()!=null && view.getPrgAction().equals("delete")){
            business.exeDelSQL(view,Integer.parseInt(view.getrowsn()));
            view.setrowsn("9999");
         }

         // 離開
         if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
             if (view.getPrgStatus()!=null && (view.getPrgStatus().equals("add"))){
                 view.setAllClear();
                 view.setButtonVisible("0");
                 view.setPrgStatus("exit");
             } else {
                view.setAllClear();
                view.setSelectShow("none");
                view.setPrgStatus("exit");
             }
         }

         // DataView
         business.setDataViewForm(DataViewForm,view);
         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
